﻿
Imports std

Public Class sequential_deliverer_wrapper
    Inherits deliverer_wrapper(Of async_deliverer_t)

    Private ReadOnly q As qless(Of [case]) = Nothing

    Public Sub New(ByVal d As async_deliverer_t)
        MyBase.New(d)
        q = New qless(Of [case])()
        assert_begin(New eventComb(Function() As Boolean
                                       Return waitfor(Function() Not q.empty()) AndAlso
                                              goto_next()
                                   End Function,
                                   Function() As Boolean
                                       Dim c As [case] = Nothing
                                       c = q.pop()
                                       If c Is Nothing Then
                                           Return goto_prev()
                                       Else
                                           Return waitfor(d.deliver(c))
                                       End If
                                   End Function))
    End Sub

    Public Overrides Function deliver(ByVal c As [case]) As eventComb
        Return New eventComb(Function() As Boolean
                                 If c Is Nothing Then
                                     Return False
                                 Else
                                     q.push(c)
                                     Return goto_end()
                                 End If
                             End Function)
    End Function
End Class
